﻿using Api.Common;
using AppManager.DAL;
using AppManager.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AppManager.BLL
{
    public class MessageBLL : BaseBLL<Message>
    {
        public static void AddOrUpdate(AddMessageModel model)
        {
            if (string.IsNullOrEmpty(model.AppName))
                throw new Exception("AppName不能為空");

            var project = ProjectBLL.GetModel(s => s.Identity == model.AppName);
            if (project == null)
                throw new Exception("AppName 不存在");

            try
            {
                JsonConvert.DeserializeObject(model.Data);
            }
            catch
            {
                model.Data = string.Concat("{\"message\":\"", model.Data, "\"}");
                //throw new Exception("Data 必須是json格式");
            }

            //将字符串转化成string[]
            var arr1 = GetArr(model.GoogleList, 1);
            var arr2 = GetArr(model.NotGoogleList, 2);
            var arr3 = GetArr(model.IosList, 3);

            var date = model.ExpirseDate == null ? DateTime.Now.AddDays(1) : model.ExpirseDate;
            Message message = new Message
            {
                Content = model.Message,
                Data = model.Data,
                ExpirseDate = date.Value,
                ProjectId = project.Id,
                MdSize = string.Format("google:{0}|signalr:{1}|ios:{2}", arr1.Count, arr2.Count, arr3.Count)
            };

            List<MessageDevice> devices = new List<MessageDevice>();
            devices.AddRange(arr1);
            devices.AddRange(arr2);
            devices.AddRange(arr3);

            //devicestr<=20;
            devices = devices.Where(s => !string.IsNullOrEmpty(s.DeviceStr))
                .Where(s => s.DeviceStr.Length > 20).ToList();

            Add(message);
            foreach (var item in devices)
            {
                item.MessageId = message.Id;
            }
            MessageDeviceBLL.AddList(devices);
        }

        public static IEnumerable<Message> GetSendingList()
        {
            return MessageDAL.GetSendingList();
        }

        private static List<MessageDevice> GetArr(string str, int status)
        {
            var result = new List<MessageDevice>();
            if (string.IsNullOrEmpty(str)) return new List<MessageDevice>(0);
            var arr = str.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var item in arr)
            {
                if (!string.IsNullOrEmpty(item))
                {
                    var type = DeviceType.unknow;
                    bool isUsegoogle = false;
                    if (status == 1)//google
                    {
                        type = DeviceType.android;
                        isUsegoogle = true;
                    }
                    else if (status == 2)//signalr
                    {
                        type = DeviceType.android;
                        isUsegoogle = false;
                    }
                    else if (status == 3)//ios
                    {
                        type = DeviceType.ios;
                        isUsegoogle = false;
                    }

                    result.Add(new MessageDevice
                    {
                        DeviceType = type,
                        IsUseGoogle = isUsegoogle,
                        DeviceStr = item,
                    });
                }
            }

            return result;
        }
    }
}
